package com.leetcode.LC_MED;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.TreeSet;

public class LC148 {
    public ListNode sortList(ListNode head) {
        if (head == null) {
            return null;
        }
        ListNode tmp = head;
        ArrayList<ListNode> list = new ArrayList<>();
        while (tmp != null) {
            list.add(tmp);
            tmp = tmp.next;
        }

        list.sort(Comparator.comparingInt(o -> o.val));
        ListNode a = list.get(0);
        for (int i = 1; i < list.size(); i++) {
            list.get(i - 1).next = list.get(i);
        }
        list.get(list.size() - 1).next = null;

        for (ListNode listNode : list) {
            System.out.print(listNode.val);
        }

        return a;
    }
}
